package cn.woblog.recyclerviewsample;

import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.support.v7.widget.helper.ItemTouchHelper;
import cn.woblog.recyclerviewsample.adapter.SwipeRecyclerViewAdapter;
import java.util.ArrayList;
import java.util.Collections;

public class SwipeRecyclerViewActivity extends AppCompatActivity {

  private ItemTouchHelper itemTouchHelper;
  private SwipeRecyclerViewAdapter adapter;
  private ArrayList<String> data;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_swipe_recycler_view);

    RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
    rv.setLayoutManager(new LinearLayoutManager(this));

    adapter = new SwipeRecyclerViewAdapter(this);
    rv.setAdapter(adapter);

    data = new ArrayList<>();
    for (int i = 0; i < 50; i++) {
      data.add("Item " + i);
    }

    adapter.setData(data);

//创建ItemTouchHelper，并设置监听器
    itemTouchHelper = new ItemTouchHelper(new MyCallback());
    itemTouchHelper.attachToRecyclerView(rv);
  }

  public void startDrag(SwipeRecyclerViewAdapter.ViewHolder viewHolder) {
    itemTouchHelper.startDrag(viewHolder);
  }

  public void startSwipe(SwipeRecyclerViewAdapter.ViewHolder viewHolder) {
    itemTouchHelper.startSwipe(viewHolder);
  }

  class MyCallback extends ItemTouchHelper.Callback {

    @Override
    public int getMovementFlags(RecyclerView recyclerView, ViewHolder viewHolder) {
      return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, ViewHolder viewHolder, ViewHolder target) {
      //如果条目类型不同，就不交换，比如上面添加了Header
      if (viewHolder.getItemViewType() != target.getItemViewType()) {
        return false;
      }

      //改变集合真是数据位置
      int sourcePosition = viewHolder.getAdapterPosition();
      int targetPosition = target.getAdapterPosition();
      Collections.swap(data, sourcePosition, targetPosition);

      //通知adapter,这两个位置的item交换位置，这样界面就能看到交换效果
      adapter.notifyItemMoved(sourcePosition, targetPosition);
      return true;
    }

    @Override
    public void onSwiped(ViewHolder viewHolder, int direction) {
      int position = viewHolder.getAdapterPosition();
      adapter.removeData(position);
    }

    @Override
    public boolean isLongPressDragEnabled() {
      return false;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
      return false;
    }

//    @Override
//    public void onSelectedChanged(ViewHolder viewHolder, int actionState) {
//      if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
//        viewHolder.itemView.setBackgroundColor(Color.RED);
////拖动时
//      } else if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
//        //滑动时
//        viewHolder.itemView.setBackgroundColor(Color.GREEN);
//      }
//      super.onSelectedChanged(viewHolder, actionState);
//    }
//
//    @Override
//    public void clearView(RecyclerView recyclerView, ViewHolder viewHolder) {
//      viewHolder.itemView.setBackgroundColor(Color.WHITE);
//      super.clearView(recyclerView, viewHolder);
//    }


    @Override
    public void clearView(RecyclerView recyclerView, ViewHolder viewHolder) {
      //这样要重置之前设置的参数
      viewHolder.itemView.setAlpha(1);
      viewHolder.itemView.setScaleX(1);
      viewHolder.itemView.setScaleY(1);
      super.clearView(recyclerView, viewHolder);
    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX,
        float dY, int actionState, boolean isCurrentlyActive) {

      if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        float progress = (float) (1 - Math.abs(dX) * 1.0 / viewHolder.itemView.getRootView()
            .getWidth());
        viewHolder.itemView.setScaleX(progress);
        viewHolder.itemView.setScaleY(progress);
        viewHolder.itemView.setAlpha(progress);
      }
      super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
  }

}
